<template>
  <div class="pay-container">
    <!-- 微信二维码支付 -->
    <div class="code">
      <img :src="payUrl" alt="" srcset="" />
    </div>
    <svg class="icon" aria-hidden="true">
      <use xlink:href="#icon-wechat_pay"></use>
    </svg>
    <span>使用微信支付</span>
    <el-descriptions title="订单详情">
      <el-descriptions-item label="订单号">
        {{ orderDetail.order_id }}
      </el-descriptions-item>
      <el-descriptions-item label="创建时间">
        {{ orderDetail.create_time }}
      </el-descriptions-item>
      <el-descriptions-item label="产品名称">
        {{ orderDetail.orderDetail[0].productName }}
      </el-descriptions-item>
      <el-descriptions-item label="商品描述">
        {{ orderDetail.orderDetail[0].description }}
      </el-descriptions-item>
      <el-descriptions-item label="购买数量">
        {{ orderDetail.orderDetail[0].quantity }}
      </el-descriptions-item>
      <el-descriptions-item label="购买用户">
        {{ orderDetail.user_nickname }}
      </el-descriptions-item>
      <el-descriptions-item label="手机号">
        {{ orderDetail.user_phone }}
      </el-descriptions-item>
      <el-descriptions-item label="总价格">
        {{ orderDetail.total_fee }}
      </el-descriptions-item>
    </el-descriptions>
  </div>
</template>
<script>
import * as api from "@/api/transaction";
//引入qrcode插件
import qrCode from "qrcode";
export default {
  data() {
    return {
      order_id: "",
      //订单详情
      orderDetail: {
        orderDetail: [
          {
            productName: "",
            description: "",
            quantity: "",
          },
        ],
      },
      //支付地址
      payUrl: "",
    };
  },
  created() {
    //订单号
    let { order_id } = this.$route.query;
    this.order_id = order_id;
    //如果用户刷新了当前页面，本次交易取消，我们需要让用户重新提交交易
    if (!this.order_id) {
      //没有订单id可能用户刷新了页面
      this.$alert("本次交易取消，请重新提交", "提示", {
        confirmButtonText: "确定",
        callback: (action) => {
          this.$router.push({ path: "/preorder" });
        },
      });
      return;
    }

    //根据订单id 请求订单详情
    //根据订单id 获取微信支付地址

    //并发请求(一次发送多个请求)
    Promise.all([
      api.getOrderDetai(this.order_id),
      api.pay(this.order_id),
    ]).then((res) => {
      console.log(res, 1);
      this.orderDetail = res[0].data.data;
      // this.order = res[0].data.data;
      console.log(this.orderDetail, "m");

      let { state } = res[1].data;
      if (state) {
        //获取支付地址
        let payUrl = res[1].data.code_url;
        //生成二维码
        qrCode.toDataURL(payUrl, (err, img) => {
          console.log(img);
          this.payUrl = img;
        });
      }
    });

    //轮询 查询订单支付状态
    this.queryStatu = setInterval(() => {
      api.getOrderStatus(this.order_id).then((res) => {
        console.log(res);
        if (res.data.state) {
          //跳转到支付成功的页面
          this.$router.push({ path: "/payment/paysuccess" });
        }
      });
    }, 10000);
  },
  beforeDestroy() {
    //清楚定时器
    clearInterval(this.queryStatu);
  },
};
</script>
